package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyTagMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.article.vo.StrategyDetailVO;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyTagService strategyTagService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private StrategyTagMapper strategyTagMapper;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }
    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }


    //攻略的保存或修改
    public void saveOrUpdate(StrategyDetail strategyDetail, String tags) {
        //获取所属目的的id
        Strategy strategy = strategyService.get(strategyDetail.getStrategy().getId());
        if ( strategy != null) {
            strategyDetail.setDest(strategy.getDest());
        }

        //作者id暂不需要设置

        //设置文章简介,截取文章的前两百字
        String content = strategyDetail.getStrategyContent().getContent();
        if (content.length() > 200) {
            strategyDetail.setSummary(content.substring(0, 200));
        } else {
            strategyDetail.setSummary(content);
        }

        //判断是否是国外
        if (strategyDetail.getDest() != null) {
            List<Destination> toasts = destinationService.getToasts(strategyDetail.getDest().getId());
            strategyDetail.setIsabroad(toasts.get(0).getId() !=  -1);
        }

        if (strategyDetail.getId() == null) {
            //创建时间
            strategyDetail.setCreateTime(new Date());
            strategyDetailMapper.insert(strategyDetail);

            //设置内容
            StrategyContent strategyContent = strategyDetail.getStrategyContent();
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.insert(strategyContent);
        } else {
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);

            //设置内容
            StrategyContent strategyContent = strategyDetail.getStrategyContent();
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.insert(strategyContent);
        }

        //处理标签
        if (tags.length() > 0) {
            String[] tagArr = tags.split(",");
            for (String tag : tagArr) {
                StrategyTag strategyTag = new StrategyTag();
                strategyTag.setName(tag);
                strategyTagMapper.insert(strategyTag);
                //处理关系表
                strategyTagMapper.insertRelation(strategyTag.getId(), strategyDetail.getId());
            }
        }

    }

    //获取攻略文章内容
    public StrategyContent getContent(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }

    public List<StrategyDetail> getTop3() {
        return strategyDetailMapper.selectTop3();
    }

    public void updateByVO(StrategyDetailVO detailVO) {
        strategyDetailMapper.updateByVO(detailVO);
    }

    public void deleteById(Long id) {
        strategyDetailMapper.deleteByPrimaryKey(id);
    }

    public void changeState(Long id, Long state) {
        strategyDetailMapper.updateState(id, state);
    }

    //查询攻略前五
    public List<StrategyDetail> getTop5() {
        return strategyDetailMapper.selectTop5();
    }

    @Override
    public List<StrategyDetail> getTTop3(Long id) {
        return strategyDetailMapper.selectTTop3(id);
    }
}
